package byj.com.meishi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import byj.com.meishi.MovieActivity;
import byj.com.meishi.R;
import byj.com.meishi.bean.Media;
import byj.com.meishi.bean.Movie;

/**
 * Created by Administrator on 2016/9/20 0020.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>{
    private List<Movie> mList;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    private final Context mContext;

    public RecyclerAdapter(List<Movie> list,Context context){
        mContext = context;
        this.mList=list;
    }
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
        final Movie movie = mList.get(position);
        String recommendCaption = movie.getRecommendCaption();
        String type = movie.getType();
        if(type.equals("scheme")){
            holder.tvCaption.setText(recommendCaption);
            holder.likeCount.setText("");
        }else if(type.equals("media")){
            Media media = movie.getMedia();
            int likecount = media.getLikecount();
            holder.tvCaption.setText(recommendCaption);
            holder.likeCount.setText(""+likecount);
        }
        String recommendCoverPic = movie.getRecommendCoverPic();
        Picasso.with(mContext).load(recommendCoverPic).centerCrop().error(R.mipmap.ic_launcher).resize(500,400).config(Bitmap.Config.ARGB_8888).placeholder(R.mipmap.a1).into(holder.icon);

        //  Glide.with(mContext).load(recommendCoverPic).into(holder.icon);
//        OkHttpClientUtils.getDataAsync(recommendCoverPic,mContext , new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                if(response.isSuccessful()){
//                    InputStream inputStream = response.body().byteStream();
//                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//                    mHandler.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            holder.icon.setImageBitmap(bitmap);
//                        }
//                    });
//                }
//            }
//        });
        holder.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, MovieActivity.class);
                Media media = movie.getMedia();
                String type1 = movie.getType();
                if(type1.equals("scheme")){

                }else{

                    String video = media.getVideo();
                    String caption = media.getCaption();
                    intent.putExtra("video",video);
                    intent.putExtra("caption",caption);
                    mContext.startActivity(intent);
                }
            }
        });
   }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        private TextView tvCaption,likeCount;
        private ImageView icon;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            tvCaption = (TextView) itemView.findViewById(R.id.tv_title);
            likeCount = (TextView) itemView.findViewById(R.id.tv_zan);
            icon = (ImageView) itemView.findViewById(R.id.iv_item);


        }
    }
}
